import time
from appium import webdriver
import pyautogui as pg
import re
import xlwings as xw


class Pull_Refund:

    def __init__(self, url):
        self.url = url

    # 进入“全国库存汇总”
    def crawler_pull(self):
        desired_caps = {
            "platformName": "Android",
            "platformVersion": "7.1.2",
            "deviceName": "xxx",
            "appPackage": "com.android.browser",
            "appActivity": ".BrowserActivity",
            "unicodeKeyboard": 'true',
            "resetKeyboard": 'true',
            "noReset": 'true',
            "newCommandTimeout": 6000,
            "automationName": "UiAutomator2"
        }

        # 连接Appium Server，初始化自动化环境
        wb = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
        wb.implicitly_wait(10)
        time.sleep(1)
        # 点击搜索框
        wb.find_element_by_id('url').click()
        wb.find_element_by_id('url').send_keys(self.url)
        # 回车，搜索
        wb.keyevent(66)
        time.sleep(2)
        # 点击账号登录
        wb.find_element_by_android_uiautomator('new UiSelector().text("账号登录").className("android.view.View")').click()
        # 滑动，滑到可以看到输入框
        time.sleep(1)
        # 输入账号
        wb.find_element_by_android_uiautomator('new UiSelector().resourceId("account").className("android.widget.EditText")').click()
        wb.find_element_by_android_uiautomator('new UiSelector().resourceId("account").className("android.widget.EditText")').send_keys('')
        # 输入密码
        wb.find_element_by_android_uiautomator('new UiSelector().resourceId("password").className("android.widget.EditText")').click()
        wb.find_element_by_android_uiautomator('new UiSelector().resourceId("password").className("android.widget.EditText")').send_keys('')
        # 点击登录
        wb.find_element_by_android_uiautomator('new UiSelector().text("登录").className("android.widget.Button")').click()
        time.sleep(3)
        # 滑动滑块
        wb.swipe(270, 1020, 810, 1020, 2000)
        time.sleep(5)
        # 关掉弹窗
        wb.find_element_by_android_uiautomator('new UiSelector().text("关闭").className("android.widget.Button")').click()
        time.sleep(1)
        # 点击左上角菜单按钮
        wb.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[1]/android.view.View/android.view.View[1]').click()
        # 点击商家进销存看板
        wb.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[1]/android.view.View[1]/android.view.View/android.view.MenuItem[9]/android.view.View').click()
        # 关掉弹窗
        wb.find_element_by_android_uiautomator('new UiSelector().text("关闭").className("android.widget.Button")').click()
        time.sleep(1)
        # 翻转模拟器
        nox_turn = pg.locateOnScreen(r'D:\nox_turn\turn.png')
        nox_highlight = pg.locateOnScreen(r'D:\nox_turn\highlight_turn.png')
        if nox_turn:
            pg.click(nox_turn)
        else:
            pg.click(nox_highlight)
        time.sleep(3)
        # 下拉页面
        for pull_down in range(5):
            wb.swipe(900, 800, 900, 100, 300)
            time.sleep(1)
        # 先调成每页100条
        wb.find_element_by_android_uiautomator('new UiSelector().text("20条/页").className("android.widget.EditText")').click()
        wb.find_element_by_android_uiautomator('new UiSelector().text("100条/页").className("android.view.View")').click()
        time.sleep(1)
        getting_data = wb.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[1]/android.view.View/android.view.View/android.view.View[1]/android.view.View/android.view.View/android.view.View/android.view.View[5]').text
        r_get_data = r'共 (.*?) 条'
        re_get_data = re.findall(r_get_data, getting_data)
        for gd_index in range(len(re_get_data)):
            re_gd = re_get_data[gd_index]
        print(re_gd)
        time.sleep(1)
        # 滑到最上方
        for pull_up in range(5):
            wb.swipe(900, 400, 900, 800, 200)
            time.sleep(1)
        # 下拉到订单显示的地方
        wb.swipe(900, 800, 900, 100, 2000)
        wb.swipe(900, 800, 900, 520, 2000)
        # 执行拉取数据的方法（函数）
        self.pull_national_data_loop(int(re_gd), wb)

    # 用于执行“全国库存汇总”的for循环
    def pull_national_data_loop(self, re_gd, wb):
        # 总体循环“re_gd_index”行
        for re_gd_index in range(int(re_gd)):
            if re_gd_index > int(re_gd)/2:
                break
            # 循环行
            for row_index in range(1):
                row_index += 1
                # 拉取列数据
                for col_index in range(9):
                    col_index += 1
                    nationwide_data = wb.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[1]/android.view.View/android.view.View/android.view.View[1]/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View[2]/android.view.View/android.widget.GridView/android.view.View[{0}]/android.view.View[{1}]'.format(row_index, col_index)).text
                    print(nationwide_data)
                # 下滑一列数据
                wb.swipe(900, 800, 900, 667, 1500)
                # 打印最后几行的数据
                if re_gd_index+8 == re_gd:
                    # 拉最后6行数据
                    for row_index2 in range(7):
                        row_index2 += 1
                        # 拉9列数据
                        for col_index2 in range(9):
                            col_index2 += 1
                            nationwide_data2 = wb.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[1]/android.view.View/android.view.View/android.view.View[1]/android.view.View/android.view.View/android.view.View/android.view.View[1]/android.view.View[3]/android.view.View[2]/android.widget.GridView/android.view.View[{0}]/android.view.View[{1}]'.format(row_index2, col_index2)).text
                            print(nationwide_data2)
        self.warehouse_goods_list(wb)

    # 仓库商品明细
    def warehouse_goods_list(self, wb):
        time.sleep(1)
        # 网页拉到最上方
        for pull_up in range(5):
            wb.swipe(900, 400, 900, 800, 200)
            time.sleep(1)
        # 把屏幕竖过来，不然定位不到数据
        nox_turn = pg.locateOnScreen(r'D:\nox_turn\turn.png')
        nox_highlight = pg.locateOnScreen(r'D:\nox_turn\highlight_turn.png')
        if nox_turn:
            pg.click(nox_turn)
        else:
            pg.click(nox_highlight)
        time.sleep(1)
        wb.find_element_by_android_uiautomator('new UiSelector().resourceId("tab-country").className("android.view.View")').click()
        time.sleep(4)
        # 下拉页面
        for pull_down in range(5):
            wb.swipe(900, 800, 900, 100, 200)
            time.sleep(1)
        # 切换每页多少条数据
        wb.find_element_by_android_uiautomator('new UiSelector().text("20条/页").className("android.widget.EditText")').click()
        time.sleep(1)
        wb.find_element_by_android_uiautomator('new UiSelector().text("100条/页").className("android.view.View")').click()
        time.sleep(5)
        # 上拉页面
        for pull_up in range(6):
            wb.swipe(900, 200, 900, 800, 200)
            time.sleep(2)
        time.sleep(1)
        # 拉到显示数据的地方
        wb.swipe(500, 1442, 500, 200, 1500)
        # 开始拉取数据,先循环行数
        for detail_index in range(8):
            # 循环列数（循环‘商品名称’和‘SKUID’）
            for name_sku_index in range(2):
                shop_name = wb.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[1]/android.view.View/android.view.View/android.view.View[1]/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View[2]/android.view.View/android.widget.GridView/android.view.View[{0}]/android.view.View[{1}]'.format(detail_index+1, name_sku_index+1)).text
                print(shop_name)
        # 循环后续数据之前，先向后拉（往前推）一条数据
        wb.swipe(500, 800, 500, 599, 1000)
        # 拉取8行后续数据,拉8行然后循环
        for follow_row_index in range(8):
            # 循环后续的列数据
            for follow_col_data in range(60):
                if follow_col_data % 4 == 0:
                    wb.swipe(800, 200, 800, 360, 1500)
                follow_data1 = wb.find_element_by_xpath('/hierarchy/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout[2]/android.widget.LinearLayout/android.widget.FrameLayout/android.webkit.WebView/android.webkit.WebView/android.view.View[1]/android.view.View/android.view.View/android.view.View[1]/android.view.View/android.view.View/android.view.View/android.view.View/android.view.View[1]/android.widget.GridView/android.view.View[{0}]/android.view.View[{1}]'.format(follow_row_index+1, follow_col_data+3)).text
                print(follow_data1)


Pull_Refund(url='https://vss-grocery.meituan.com').crawler_pull()
